<?php
/*
*    @todo    实现电子合同签章过程 
*/
namespace tech; //  公司签章2.1版本
require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once dirname(dirname(__FILE__)) . '/include/econtract_v2.class.php';


$type = $data['type'] = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';	//签署类型，0客户签 1公司签 
$filename = $data['filename'] = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';	//查看pdf的文件名称
$cust_id = $data['cust_id'] = isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';	//资源ID
$act = $data['act'] = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';	//操作，view在线查看pdf，在线查看pdf的图片格式，download下载pdf
$name = $data['name'] =  isset($_REQUEST['name']) ? $_REQUEST['name'] : '';	//要查看或下载的文件名
$idcard = $data['idcard'] =  isset($_REQUEST['idcard']) ? $_REQUEST['idcard'] : '';	//要查看或下载的文件名
$mobile = $data['mobile'] =  isset($_REQUEST['mobile']) ? $_REQUEST['mobile'] : '';	//要查看或下载的文件名
$accountId = $data['accountId'] = isset($_REQUEST['accountId']) ? $_REQUEST['accountId'] : '';	//要查看或下载的文件名
$color = $data['color'] = isset($_REQUEST['color']) ? $_REQUEST['color'] : '';	//要查看或下载的文件名
$hText = $data['hText'] = isset($_REQUEST['hText']) ? $_REQUEST['hText'] : '';	//要查看或下载的文件名
$qText = $data['qText'] = isset($_REQUEST['qText']) ? $_REQUEST['qText'] : '';	//要查看或下载的文件名
$sealData = isset($_REQUEST['sealData']) ? $_REQUEST['sealData'] : '';	//要查看或下载的文件名
$data['sealData'] = urlencode($sealData);
$sealData = urldecode($sealData);
$srcPdfFile = $data['srcPdfFile'] = isset($_REQUEST['srcPdfFile']) ? $_REQUEST['srcPdfFile'] : '';	//要查看或下载的文件名
$dstPdfFile = $data['dstPdfFile'] = isset($_REQUEST['dstPdfFile']) ? $_REQUEST['dstPdfFile'] : '';	//要查看或下载的文件名
$signType = $data['signType'] = isset($_REQUEST['signType']) ? $_REQUEST['signType'] : '';	//要查看或下载的文件名
$posPage = $data['posPage'] = isset($_REQUEST['posPage']) ? $_REQUEST['posPage'] : '';	//要查看或下载的文件名
$posX = $data['posX'] = isset($_REQUEST['posX']) ? $_REQUEST['posX'] : '';	//要查看或下载的文件名
$posY = $data['posY'] = isset($_REQUEST['posY']) ? $_REQUEST['posY'] : '';	//要查看或下载的文件名
$key = $data['key'] = isset($_REQUEST['key']) ? $_REQUEST['key'] : '';	//要查看或下载的文件名
$once = $data['once'] = isset($_REQUEST['once']) ? $_REQUEST['once'] : '';	//时间戳，用来判断有效期
$personarea = $data['personarea'] = isset($_REQUEST['personarea']) ? $_REQUEST['personarea'] : '';	//法人或者客户所在区域
$usertype = $data['usertype'] = isset($_REQUEST['usertype']) ? $_REQUEST['usertype'] : '';	//注册类型，1-代理人注册，2-法人注册，默认1（可空）
$agentname = $data['agentname'] = isset($_REQUEST['agentname']) ? $_REQUEST['agentname'] : '';	//代理人姓名，当注册类型为1时必填
$agentIdNo = $data['agentidno'] = isset($_REQUEST['agentidno']) ? $_REQUEST['agentidno'] : '';	//代理人身份证号，当注册类型为1时必填
$legalName = $data['legalname'] = isset($_REQUEST['legalname']) ? $_REQUEST['legalname'] : '';	//代理人姓名，当注册类型为1时必填
$legalIdNo = $data['legalidno'] = isset($_REQUEST['legalidno']) ? $_REQUEST['legalidno'] : '';	//代理人身份证号，当注册类型为1时必填
$width = $data['width'] = isset($_REQUEST['width']) ? $_REQUEST['width'] : '';	//印章宽度
$templateType = $data['templateType'] = isset($_REQUEST['templateType']) ? $_REQUEST['templateType'] : '';	//客户印章，SQUARE（正方形），RECTANGLE（长方形）

if (!check_signature_str($data))
{
    if ($act == 'view') {
        header("Content-type: text/html; charset=utf-8");
        exit('验签失败！');
    } else {
        exit(json_encode(array('code' => 0, 'msg' => '验签失败！')));
    }
}
else
{
    if(!file_exists(PDF_ROOT_PATH) && !is_dir(PDF_ROOT_PATH)){
        mkdir(PDF_ROOT_PATH); 
    }  
    $file_dir = PDF_ROOT_PATH . $cust_id . '/';
    $srcPdf = $file_dir . $srcPdfFile;// 原始文件
    $first = $file_dir . $dstPdfFile;//第一次签章后文件 
    $file_dir = PDF_ROOT_PATH . $cust_id . '/';
    $filepath = $file_dir . $filename;
    $econtract = new Econtract_V2();
    switch ($act)
    {
        case 'addAccount':
            $res = $econtract->addAccount($type, $name, $idcard, $mobile);
            if($res['errCode'] == 0){
                $result = $res['accountid'];
            }else{
                $result = json_encode($res);
            }
            echo $result;
            break;
        case 'addTemplateSeal':
            $res = $econtract->addTemplateSeal($type, $accountId, $color, $hText, $qText, $templateType);
            if($res['errCode'] == 0){
                $result = $res['sealData'];
            }else{
                $result = json_encode($res);
            }
            echo $result;
            break;
        case 'userSign':
            $res = $econtract->userSign($accountId, $sealData, $srcPdf, $first, $signType, $posPage, $posX, $posY, $key, $width);
            if($res['errCode'] == 0){
                $result = 1;
            }else{
                $result = json_encode($res);
            }
            echo $result;
            break;
        case 'acctdel':
            $res = $econtract->accountDelete($accountId);
            if($res['errCode'] == 0){
                $result = 1;
            }else{
                $result = json_encode($res);
            }
            echo $result;
            break;
        case 'getacctinfo':
            if ($type == 0)
            {
                $res = $econtract->getAccountInfoByIdNo($idcard, 11); //账户证件类型，11-大陆身份证号，12-香港通行证，13-澳门通行证，14-台湾通行证，15-外籍证件
            }
            elseif($type == 1)
            {
                $res = $econtract->getAccountInfoByIdNo($idcard, 22); //21-组织机构代码号，22-统一社会信用代码，23-工商注册号
            }
            if($res['errCode'] == 0){
                $result = json_encode($res['accountInfo']);
            }else{
                $result = json_encode($res);
            }
            echo $result;
            break;
        default:
            $result = false;
            break;
    }
}

function check_signature_str($data = array())
{
    $commonstr = "econtractview";
    $signature = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';
    $string = md5(implode(array(strtoupper($data['act']), $commonstr)));
    if(!empty($data) && is_array($data))
    {
        ksort($data);
        foreach($data as $key => $value) 
        {
            $string .= urldecode($value);
        }
        return md5($string) == $signature ? true : false;
    }
    else
    {
        return false;
    }
}
exit;
?>